package com.itheima.mall.web.ums;


import com.itheima.mall.commons.page.PageResult;
import com.itheima.mall.commons.result.Result;
import com.itheima.mall.dubbo.api.ums.ResourceService;
import com.itheima.mall.entity.domain.ums.Resource;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.Path;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 后台资源表 前端控制器
 * </p>
 *
 * @author qinglang
 * @since 2022-07-05
 */
@RestController
@RequestMapping("/resource")
@CrossOrigin(origins = "*")
public class ResourceController {

    @DubboReference
    private ResourceService resourceService;

    /**
     * 添加后台资源
     * @param resource
     * @return
     */
    @PostMapping("/create")
    public Result create(@RequestBody Resource resource){
        resourceService.create(resource);
        return Result.success();
    }

    /**
     * 根据ID删除后台资源
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@PathVariable Integer id){
        resourceService.delete(id);
        return Result.success();
    }

    /**
     * 分页模糊查询后台资源
     * @param
     */
    @GetMapping("/list")
    public Result list(Integer categoryId,String nameKeyword, Integer pageNum, Integer pageSize,String urlKeyword){
        PageResult pageResult = resourceService.getList(categoryId,nameKeyword,pageNum,pageSize,urlKeyword);
        return Result.success(pageResult);
    }

    /**
     * 查询所有后台资源
     * @return
     */
    @GetMapping("/listAll")
    public Result listAll(){
        List<Resource> resources = resourceService.listAll();
        return Result.success(resources);
    }

    /**
     * 修改后台资源
     * @param id
     * @param resource
     * @return
     */
    @PostMapping("/update/{id}")
    public Result update(@PathVariable Integer id, @RequestBody Resource resource){
        resource.setId(id);
        resourceService.updateRs(resource);
        return Result.success();
    }

    /**
     * 根据ID获取资源详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id){
        Resource resource = resourceService.get(id);
        return Result.success(resource);
    }
}

